﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluorineFx.Messaging;
using FluorineFx;
using System.Collections;

namespace Htxw.AMFRemoting
{
    /// <summary>
    /// HT错误异常类，用于抛异常和记录日志
    /// </summary>
    public class HtException : MessageException
    {
        /// <summary>
        /// WS错误字串
        /// </summary>
        public const string WsErrorMessageFormat = "WebService Error: {0}";
        /// <summary>
        /// DS错误字串
        /// </summary>
        public const string DSErrorMessageFormat = "DataService Error: {0}";
        /// <summary>
        /// DB错误字串
        /// </summary>
        public const string DBErrorMessageFormat = "Database Error: {0}";
        /// <summary>
        /// LOG记录日志对象
        /// </summary>
        // public static readonly ILog Log = LogManager.GetLogger(typeof(HtException));

        ///<summary>
        ///</summary>
        public HtException()
            : base()
        {

        }

        ///<summary>
        ///</summary>
        ///<param name="message"></param>
        public HtException(string message)
            : base(message)
        {
            //if (HtConfig.IsDebug)
            //    Log.Error("message::" + message);
            AddErrorInfo(message);
        }

        ///<summary>
        ///</summary>
        ///<param name="message"></param>
        ///<param name="inner"></param>
        public HtException(string message, Exception inner)
            : base(message, inner)
        {
            //if (HtConfig.IsDebug)
            //    Log.Error("message::" + message);
            AddErrorInfo(inner);
        }

        ///<summary>
        ///</summary>
        ///<param name="inner"></param>
        public HtException(Exception inner)
            : base(inner)
        {
            //if (HtConfig.IsDebug)
            //    Log.Error("message::" + inner.Message);
            AddErrorInfo(inner);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="errorType">错误类型，DS/WS/DB错误字串</param>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息描述</param>
        /// <param name="source">错误源</param>
        /// <param name="tmpObject">错误包 对象，用于发送到客户端</param>
        public HtException(string errorType, int code, string message, string source, ASObject tmpObject)
            : base(tmpObject, message)
        {
            //if (HtConfig.IsDebug)
            //    Log.Error("WS Exception : code::" + code + "  message::" + message + "  source::" + source);

            //AddErrorInfo(errorType, code, message, source);
            AddErrorInfo(message, code, source);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="errorType">错误类型，DS/WS/DB错误字串</param>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息描述</param>
        /// <param name="source">错误源</param>
        /// <param name="inner">捕获的Exception对象</param>
        /// <param name="tmpObject">错误包 对象，用于发送到客户端</param>
        public HtException(string errorType, int code, string message, string source, Exception inner, ASObject tmpObject)
            : base(tmpObject, message, inner)
        {
            //if (HtConfig.IsDebug)
            //    RwLog(code, message, source, tmpObject, inner);

            AddErrorInfo(inner, code, source);
        }




        /// <summary>
        /// 添加错误信息，到异常类中
        /// </summary>
        /// <param name="err"></param>
        /// <param name="code">错误码</param>
        /// <param name="source">错误源</param>
        public void AddErrorInfo(Exception err, int code, string source)
        {
            Source = source;
            HelpLink = "http://www.hylend.com/";
            FaultCode = code.ToString();
            RootCause = err;
        }

        /// <summary>
        /// 添加错误信息，到异常类中
        /// </summary>
        public void AddErrorInfo(Exception err)
        {
            HelpLink = "http://www.hylend.com/";
            RootCause = err;
        }

        /// <summary>
        /// 添加错误信息，到异常类中
        /// </summary>
        public void AddErrorInfo(string msg)
        {
            HelpLink = "http://www.hylend.com/";
            RootCause = msg;
        }

        /// <summary>
        /// 添加错误信息，到异常类中
        /// </summary>
        public void AddErrorInfo(string msg, int code, string source)
        {
            Source = source;
            HelpLink = "http://www.hylend.com/";
            FaultCode = code.ToString();
            RootCause = msg;
        }

        ///// <summary>
        ///// 记录错误日志
        ///// </summary>
        ///// <param name="code">错误码</param>
        ///// <param name="message">错误消息描述</param>
        ///// <param name="source">错误源</param>
        ///// <param name="o">错误包 对象</param>
        ///// <param name="ex">捕获的Exception对象</param>
        //public void RwLog(int code, string message, string source, ASObject o, Exception ex)
        //{
        //    var s = new StringBuilder();
        //    IDictionaryEnumerator en = o.GetEnumerator();

        //    while (en.MoveNext())
        //    {
        //        s.AppendLine(" Key::" + en.Key + "\n Value:: " + en.Value + "");
        //    }
        //    //Log.Error("WS Exception : code::" + code + "  message::" + message + "  source::" + source + "\n" + s, ex);
        //}


        /// <summary>
        /// 生成一个新的HtException对象
        /// </summary>
        /// <param name="errorType">错误类型，DS/WS/DB错误字串</param>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息描述</param>
        /// <param name="source">错误源</param>
        /// <returns>一个新的HtException对象</returns>
        public static HtException GetException(string errorType, int code, string message, string source)
        {
            var o = GetExceptionObjcet(code, message);
            return new HtException(errorType, code, message, source, o);
        }

        /// <summary>
        /// 根据捕获的Exception对象生成一个新的HtException对象
        /// </summary>
        /// <param name="errorType">错误类型，DS/WS/DB错误字串</param>
        /// <param name="code">错误码</param>
        /// <param name="message">错误消息描述</param>
        /// <param name="source">错误源</param>
        /// <param name="inner">捕获的Exception对象</param>
        /// <returns>一个新的HtException对象</returns>
        public static HtException GetException(string errorType, int code, string message, string source, Exception inner)
        {
            var o = GetExceptionObjcet(code, message, inner);
            return new HtException(errorType, code, message, source, inner, o);
        }

        /// <summary>
        /// 生成一个新的错误包 对象
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="what">错误消息描述</param>
        /// <returns>错误包 对象</returns>
        public static ASObject GetExceptionObjcet(int code, string what)
        {
            var tmpobject = new ASObject();
            tmpobject["code"] = code;
            tmpobject["what"] = what;
            return tmpobject;
        }

        /// <summary>
        /// 根据捕获的Exception对象生成一个新的错误包 对象
        /// </summary>
        /// <param name="code">错误码</param>
        /// <param name="what">错误消息描述</param>
        /// <param name="e">捕获的Exception对象</param>
        /// <returns>错误包 对象</returns>
        public static ASObject GetExceptionObjcet(int code, string what, Exception e)
        {
            var tmpobject = new ASObject();
            tmpobject["code"] = code;
            tmpobject["what"] = what;
            tmpobject["e"] = e;
            return tmpobject;
        }
    }
}
